import { Injectable } from '@angular/core';
import { HttpClient } from "@angular/common/http";

import { encrypt } from "../../assets/js/rsaEncrypt.js";
import { Observable } from 'rxjs';
import { tap } from 'rxjs/operators';


@Injectable({
    providedIn: 'root'
})
export class LoginService {
    private baseUrl:string = "/auth/login";

    constructor(
        private http : HttpClient,
    ) { }

    login(data) : Observable<any>{
        let _dataJson = {
            password : encrypt(data.password),
            username : data.userName
        }
        return this.http.post( this.baseUrl, _dataJson ).pipe(
            tap(res=>{
                localStorage.setItem("roles", JSON.stringify(res.user.roles));
            })
        );
    }
}
